import requests
from urllib.parse import urlencode
import json
import save_poi_data
import time


def send_get_request(url, json_params):
    # Convert JSON object to URL parameters
    params = urlencode(json_params)

    # Combine the URL with parameters
    full_url = f"{url}?{params}"

    # Send GET request
    response = requests.get(full_url)

    # Return the response
    return response.text


def get_poi_data(nb_x,nb_y):
    url = "https://map.baidu.com/"
    json_params = {
        "newmap": "1",
        "reqflag": "pcmap",
        "biz": "1",
        "from": "webmap",
        "da_par": "direct",
        "pcevaname": "pc4.1",
        "qt": "spot",
        "c": "223",
        "wd": "美食",
        "wd2": "",
        "pn": "0",
        "nn": "0",
        "db": "0",
        "sug": "0",
        "addr": "0",
        "pl_data_type": "cater",
        "pl_sub_type": "餐馆",
        "pl_price_section": "0,+",
        "pl_sort_type": "data_type",
        "pl_sort_rule": "0",
        "pl_discount2_section": "0,+",
        "pl_groupon_section": "0,+",
        "pl_cater_book_pc_section": "0,+",
        "pl_hotel_book_pc_section": "0,+",
        "pl_ticket_book_flag_section": "0,+",
        "pl_movie_book_section": "0,+",
        "pl_business_type": "cater",
        "pl_business_id": "",
        "da_src": "pcmappg.poi.page",
        "on_gel": "1",
        "src": "7",
        "gr": "3",
        "l": "16",
        "nb_x": "13312808.307571348",
        "nb_y": "4030517.4705482814",
        "uid": "638d4e5d7edf1ace84154117",
        "gr_radius": "1000",
        "rn": "50",
        "tn": "B_NORMAL_MAP",
        "auth": "MI8yXSZB8vKc08VxOcTHEKOA0N=ZY3gBuxNzHExNVLHtN2331NAN5x2Cwxz6ZwWvvkGcuVtvvhguVtvyheuVtvCMGuVtvCQMuVtvIPcuxtw8wkv7uvZgMuVtv@vcuVtvc3CuVtvcPPuVtcvY1SGpuxEtHrQH2YYG3uyWJLIwiKDv7uvhgMuzVVtvrMhuxBxtOmm0mEb1egvcguxNzHExLTNT",
        "seckey": "O/WgqMwdsCUMrVu7ePnq88QEMmYpBS53av6oBTs+87g=,O_WgqMwdsCUMrVu7ePnq8z4u0aLa9DnKHpcGEsIXKlaNdnF6lGrn2BLJegMvi76kiy4fyueVWPS3Sq6hhtUYLkKuE7cNCLB4JAJXMm5tXwpp_9c0_JKB1pnH7LnLjsXSYD_HhL37Q-cc6aK9Uz8vMnDFwwfFuuG5FkX4OVDHWIywJRaSUPxzRb6Z4Fkzx3rP",
        "ie": "utf-8",
        "b": "(13309608.307571348,4029009.4705482814;13316008.307571348,4032025.4705482814)",
        "t": "1725507034132",
        "newfrom": "zhuzhan_webmap"
    }
    milliseconds = int(time.time() * 1000)
    json_params['t'] = milliseconds
    json_params['nb_x'] = nb_x
    json_params['nb_y'] = nb_y
    x1 = nb_x-3200
    y1 = nb_y-1508
    x2 = nb_x+3200
    y2 = nb_y+1508
    json_params['b'] = f"({x1},{y1};{x2},{y2})"
    result = send_get_request(url, json_params)
    reponseJson = json.loads(result)
    if reponseJson.get("content") is not None:
        save_poi_data.process_poi_data(reponseJson.get("content"))
    else:
        print("content属性不存在")
    print(result)


def test():
    url = "https://map.baidu.com/"
    json_params = {
        "newmap": "1",
        "reqflag": "pcmap",
        "biz": "1",
        "from": "webmap",
        "da_par": "direct",
        "pcevaname": "pc4.1",
        "qt": "spot",
        "c": "223",
        "wd": "美食",
        "wd2": "",
        "pn": "0",
        "nn": "0",
        "db": "0",
        "sug": "0",
        "addr": "0",
        "pl_data_type": "cater",
        "pl_sub_type": "餐馆",
        "pl_price_section": "0,+",
        "pl_sort_type": "data_type",
        "pl_sort_rule": "0",
        "pl_discount2_section": "0,+",
        "pl_groupon_section": "0,+",
        "pl_cater_book_pc_section": "0,+",
        "pl_hotel_book_pc_section": "0,+",
        "pl_ticket_book_flag_section": "0,+",
        "pl_movie_book_section": "0,+",
        "pl_business_type": "cater",
        "pl_business_id": "",
        "da_src": "pcmappg.poi.page",
        "on_gel": "1",
        "src": "7",
        "gr": "3",
        "l": "16",
        "nb_x": "13312808.307571348",
        "nb_y": "4030517.4705482814",
        "uid": "638d4e5d7edf1ace84154117",
        "gr_radius": "1000",
        "rn": "50",
        "tn": "B_NORMAL_MAP",
        "auth": "MI8yXSZB8vKc08VxOcTHEKOA0N=ZY3gBuxNzHExNVLHtN2331NAN5x2Cwxz6ZwWvvkGcuVtvvhguVtvyheuVtvCMGuVtvCQMuVtvIPcuxtw8wkv7uvZgMuVtv@vcuVtvc3CuVtvcPPuVtcvY1SGpuxEtHrQH2YYG3uyWJLIwiKDv7uvhgMuzVVtvrMhuxBxtOmm0mEb1egvcguxNzHExLTNT",
        "seckey": "O/WgqMwdsCUMrVu7ePnq88QEMmYpBS53av6oBTs+87g=,O_WgqMwdsCUMrVu7ePnq8z4u0aLa9DnKHpcGEsIXKlaNdnF6lGrn2BLJegMvi76kiy4fyueVWPS3Sq6hhtUYLkKuE7cNCLB4JAJXMm5tXwpp_9c0_JKB1pnH7LnLjsXSYD_HhL37Q-cc6aK9Uz8vMnDFwwfFuuG5FkX4OVDHWIywJRaSUPxzRb6Z4Fkzx3rP",
        "ie": "utf-8",
        "b": "(13309608.307571348,4029009.4705482814;13316008.307571348,4032025.4705482814)",
        "t": "1725507034132",
        "newfrom": "zhuzhan_webmap"
    }
    milliseconds = int(time.time() * 1000)
    json_params['t'] = milliseconds
    result = send_get_request(url, json_params)
    reponseJson = json.loads(result)
    if reponseJson.get("content") is not None:
        save_poi_data.process_poi_data(reponseJson.get("content"))
    else:
        print("content属性不存在")
    print(result)


if __name__ == "__main__":
    get_poi_data(13312808.307571348,4030517.4705482814)
